package com.cy.lib.manager;/**
 * Created by CY on 2015/4/20.
 */

import android.app.Application;

import java.util.HashMap;
import java.util.Map;

/**
 * @author CY
 * @date 2015/4/20
 * @qq 568305883
 * @email tmxdyf@163.com
 */
public class Manager {


    public enum LocalService {
        DATABASE, LIFE, NETWORK, STORAGE, ACCOUNT;
    }

    private Application mApp;
    private static Manager mManager;
    private static Map<LocalService, Object> mLocalServiceMap;
    public LifeManager mLifeManager;
    public DataManager mDataManager;
    public AccountManager mAccountManager;
    public NetWorkManager mNetWorkManager;
    public StorageManager mStorageManager;

    public boolean isConnectivity;

    protected Manager() {
//        init();
    }

    public static Manager getInstance() {
        if (mManager == null)
            mManager = new Manager();
        return mManager;
    }

    public void init(Application app) {
        mApp = app;
        mNetWorkManager = (NetWorkManager) getLocalService(LocalService.NETWORK, app);
        isConnectivity = mNetWorkManager.mNetState == NetWorkManager.NetState.connected;
        mLifeManager = (LifeManager) getLocalService(LocalService.LIFE, app);
        mAccountManager = (AccountManager) getLocalService(LocalService.ACCOUNT, app);
        mDataManager = (DataManager) getLocalService(LocalService.DATABASE, app);
        mStorageManager = (StorageManager) getLocalService(LocalService.STORAGE, app);
    }

    public Object getLocalService(LocalService localService) {
        return getLocalService(localService, mApp);
    }

    /**
     * 获取本地服务
     *
     * @param localService
     * @return
     */
    public static Object getLocalService(LocalService localService, Application app) {
        if (mLocalServiceMap == null) {
            mLocalServiceMap = new HashMap<LocalService, Object>();
        }
        if (mLocalServiceMap.containsKey(localService)) {
            return mLocalServiceMap.get(localService);
        } else {
            switch (localService) {
                case LIFE:
                    mLocalServiceMap.put(localService, new LifeManager(app));
                    break;
                case STORAGE:
                    mLocalServiceMap.put(localService, new StorageManager(app));
                    break;
                case DATABASE:
                    mLocalServiceMap.put(localService, new DataManager(app));
                    break;
                case NETWORK:
                    mLocalServiceMap.put(localService, new NetWorkManager(app));
                    break;
                case ACCOUNT:
                    mLocalServiceMap.put(localService, new AccountManager(app));
                    break;
            }
        }

        return mLocalServiceMap.get(localService);
    }
}
